package com.tdg.qywx.utils;


import net.sf.json.JSONObject;
import org.apache.commons.lang.RandomStringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Utils - 微信操作工具类
 * add by sl on 2015-12-04 09:42:34
 * @author  mt
 * @version 1.0
 */
@Component
public final class WeiXinUtils2 {

    //private static Logger logger = Logger.getLogger(WeiXinUtils2.class);

    //企业微信应用
    public static final String APPID = "wwf4e0e3325668447b";

    public static String APP_SECRET;

    @Value("${wx.app.secret}")
    public void setAPP_SECRET(String value) {
        WeiXinUtils2.APP_SECRET = value;
    }
    public static String getAPP_SECRET() {
        return WeiXinUtils2.APP_SECRET;
    }

    //正式环境
//    public static final String APP_SECRET ="lc2sAPu2BmXQBEdgZHk3HLlWdXvEKE_7VUVDHzG7UOE";
    //测试环境
//    public static final String APP_SECRET ="CueUgSYVWH5s7Eelz1xHfEV4fTqq91xor7FINEoJ_zY";

    //public static final String redirect_uri = "http://oa.tdgcore.com:8070/tdg/mocks/shouye.jsp";
    public static final String redirect_uri = "http://oa.tdgcore.com:8999/wxapi/wxclientmenu/36418613af684d31921ad997887f8e70";
    public static final String AgentId = "1000063";
    private static final Map map = new HashMap();


    /**
     * 获取accessToken
     * @return accessToken
     */
    public static String getAccess_token(String wxAppId, String secret) {
//      String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+ wxAppId +"&secret=" + secret;
        String url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid="+ wxAppId +"&corpsecret="+ secret;
        JSONObject jsonobject = HttpClientUtil.sendGet(url, null);
        String url1 = "https://open.weixin.qq.com/connect/oauth2/authorize?appid="+wxAppId+"&redirect_uri="+redirect_uri+"&response_type=code&scope=snsapi_userinfo&agentid="+AgentId+"&state=123#wechat_redirect";
        JSONObject jsonobject1 = HttpClientUtil.sendGet(url1, null);
        return jsonobject.getString("access_token");
    }

    /**
     * 获取jsapi_ticket
     * @author mt
     * @time 2017年4月4日21:50:52
     * @return
     */
    public static String getJsapiTicket(){
        String accessToken = "";
        accessToken = getAccess_token(APPID, getAPP_SECRET());
       // System.out.println("accessToken:"+accessToken);
//                String url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token="+accessToken+"&type=jsapi";
        String url = "https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket?access_token="+accessToken+"&type=jsapi";
        JSONObject jsonobject = HttpClientUtil.sendGet(url, null);
       // System.out.println("jsonobject:"+jsonobject);
        return jsonobject.getString("ticket");
    }

    /**
     * 获取微信签名signature
     * @param url jsapi_ticket
     * @return signature
     * @throws IOException
     */
    public Map<String, Object> getSignature(String url) throws IOException {
        Map<String, Object> wxMap = new HashMap<String, Object>();
        String jsapi_ticket = "";
        Long timeJt = (Long) map.get("timeJt");
        jsapi_ticket = (String) map.get("jsapi_ticket");
        Long nowDateJt = new Date().getTime();
        if (jsapi_ticket != null && timeJt != null && ((nowDateJt - timeJt) < 7200 * 1000L)) {
            //System.out.println("jsapi_ticket 存在");
            //System.out.println("nowDateJt - timeJt=="+(nowDateJt - timeJt));
        } else {
            jsapi_ticket = getJsapiTicket();
            //System.out.println("jsapi_ticket 获取");
            map.put("jsapi_ticket",jsapi_ticket);
            map.put("timeJt",nowDateJt);
        }

        //随机字符串和时间戳
        String new_novestr = RandomStringUtils.randomAlphanumeric(16); //随机字符串
        String timestamp = String.valueOf(System.currentTimeMillis() / 1000);//时间戳
        String signature = "";
        // 拼接字符串
        String string1 = "jsapi_ticket=" + jsapi_ticket + "&noncestr=" + new_novestr + "&timestamp=" + timestamp + "&url=" + url;
        signature = CheckUtil.SHA1(string1);
        //System.out.println("签名=="+signature);

        wxMap.put("noncestr", new_novestr);
        wxMap.put("timesTamp", timestamp);
        wxMap.put("signature", signature);
        wxMap.put("appId", APPID);
        return wxMap;
    }
}

